#!/bin/bash
# Author: fanyongfeng
# 安装kibana
# 操作系统：centos7/银河麒麟v10-sp2

# 使用root用户执行
[[ $(id -u) -ne 0 ]] && echo "请使用root用户执行此脚本" && exit 1

usage(){
    echo "Usage:"
    echo "sh $0
            --prefix=安装目录(默认/data/kibana-\${VERSION})
            --version=安装版本(默认6.8.22)
            --es_ip_list=es地址(默认localhost:9200,多个地址用逗号分隔)
            --es_username=es连接用户名(默认为空)
            --es_password=es连接密码(默认为空)
        "
    echo "示例:"
    echo "sh $0 --version=6.8.22 --es_ip_list=192.168.100.101:9200,192.168.100.102:9200,192.168.100.103:9200 --es_username=elastic --es_password=password"
    exit 1
}

#处理参数，规范化参数
ARGS=`getopt -o h --long prefix:,version:,es_ip_list:,help -n "$0" -- "$@"`
if [[ $? != 0 || $# -eq 0 ]]
then
        usage
fi

#重新排列参数顺序
eval set -- "${ARGS}"
#通过shift和while循环处理参数
while true
do
    case $1 in
        --prefix)
            PREFIX=$2
            shift 2
            ;;
        --version)
            VERSION=$2
            shift 2
            ;;
        --es_ip_list)
            ES_IP_LIST=$2
            shift 2
            ;;
        --es_username)
            ES_USERNAME=$2
            shift 2
            ;;
        --es_password)
            ES_PASSWORD=$2
            shift 2
            ;;
        -h|-help)
            usage
            ;;
         --)
            shift
            break
            ;;
         *)
            echo "Internal error!"
            exit 1
            ;;
    esac
done

INSTALL_DIR=${PREFIX:-/data/kibana-${VERSION}}
VERSION=${VERSION:-6.8.22}
ES_IP_LIST=${ES_IP_LIST:-localhost:9200}
RUN_USER="kibana"
HOST_IP=$(ip a | grep global | head -1 | awk -F'/' '{print $1}' | awk '{print $2}')

# 下载kibana
if [[ ! -f kibana-${VERSION}-linux-x86_64.tar.gz ]]
then
    echo "正在下载安装包..."
    wget -4 https://artifacts.elastic.co/downloads/kibana/kibana-${VERSION}-linux-x86_64.tar.gz
    if [[ $? -ne 0 ]]
    then
        echo "kibana-${VERSION}-linux-x86_64.tar.gz下载失败，请重试或手动上传此安装包"
        exit 1
    fi
fi

# 安装kibana
echo "正在解压安装包..."
tar zxf kibana-${VERSION}-linux-x86_64.tar.gz && mv kibana-${VERSION}-linux-x86_64 ${INSTALL_DIR}

# 添加用户并授权
useradd ${RUN_USER} -s /sbin/nologin
chown -R ${RUN_USER}.${RUN_USER} ${INSTALL_DIR}

# 修改kibana配置文件
ES_IP_LIST=$(echo "${ES_IP_LIST}" | sed 's/ //g' | sed 's#,#", "http://#g'|sed 's#^#"http://#' | sed 's/$/"/')
\cp ${INSTALL_DIR}/config/kibana.yml ${INSTALL_DIR}/config/kibana.yml.bak
cat > ${INSTALL_DIR}/config/kibana.yml <<EOF
server.host: "${HOST_IP}"
server.name: kibana
elasticsearch.hosts: [${ES_IP_LIST}]
xpack.monitoring.ui.container.elasticsearch.enabled: true
i18n.locale: "zh-CN"
elasticsearch.username: ${ES_USERNAME}
elasticsearch.password: ${ES_PASSWORD}
EOF

if [[ -z ${ES_USERNAME} || -z ${ES_PASSWORD} ]]
then
    sed -i '/^elasticsearch.username:/s/^/#/' ${INSTALL_DIR}/config/kibana.yml
    sed -i '/^elasticsearch.password:/s/^/#/' ${INSTALL_DIR}/config/kibana.yml
fi

# 使用systemd管理kibana
cat > /etc/systemd/system/kibana.service << EOF
[Unit]
# 服务描述
Description=kibana
After=network.target

[Service]
User=kibana
Group=kibana
ExecStart=${INSTALL_DIR}/bin/kibana
ExecReload=/bin/kill -HUP \$MAINPID
KillMode=process
Restart=on-failure
RestartSec=30s
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

# 启动kibna
echo "安装完成，正在启动kibana..."
systemctl daemon-reload
systemctl enable kibana
systemctl start kibana
systemctl status kibana
